//
//  MBProgressHUD+MJ.m
//
//  Created by mj on 13-4-18.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "MBProgressHUD+MJ.h"

@implementation MBProgressHUD (MJ)
#pragma mark 显示信息
+ (void)show:(NSString *)text icon:(NSString *)icon view:(UIView *)view
{
    if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.labelText = text;
    // 设置图片
    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"MBProgressHUD.bundle/%@", icon]]];
    // 再设置模式
    hud.mode = MBProgressHUDModeCustomView;
    
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    
    // 1秒之后再消失
    [hud hide:YES afterDelay:0.7];
}

#pragma mark 显示错误信息
+ (void)showError:(NSString *)error toView:(UIView *)view{
    [self show:error icon:@"error.png" view:view];
}

+ (void)showSuccess:(NSString *)success toView:(UIView *)view
{
    [self show:success icon:@"success.png" view:view];
}

#pragma mark 显示一些信息
+ (MBProgressHUD *)showMessage:(NSString *)message toView:(UIView *)view {
    if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
    // 快速显示一个提示信息
    MBProgressHUD *progressHud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    progressHud.backgroundColor = UIColorFromRGB(0xf6f6f6);
    UIImageView *gifView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    gifView.contentMode = UIViewContentModeScaleAspectFit;
    NSMutableArray *animationImgs = [NSMutableArray array];
    
    for (int i = 1; i <= 8; i ++) {
        NSString *str = i < 10 ? [NSString stringWithFormat:@"0%d",i] : [NSString stringWithFormat:@"%d",i];
        NSString *imageName = [NSString stringWithFormat:@"window_loading%@.png",str];
        UIImage *image = [UIImage imageNamed:imageName];
        [animationImgs addObject:image];
    }
    
    
    gifView.animationImages = animationImgs;
    gifView.animationRepeatCount = 0;
    gifView.animationDuration = 1.0f;
    gifView.width = 100;
    gifView.height = 66;
    [gifView startAnimating];

    progressHud.mode = MBProgressHUDModeCustomView;
    progressHud.customView = gifView;
    progressHud.customView.width = 80;
    progressHud.customView.height = 45;
    progressHud.color = [UIColor clearColor];
    
//    progressHud.labelText = @"正在加载中..";
//    progressHud.labelColor = [UIColor grayColor];
//    hud.labelText = message;
    // 隐藏时候从父控件中移除
    progressHud.removeFromSuperViewOnHide = YES;
    // YES代表需要蒙版效果
//    progressHud.dimBackground = YES;
    return progressHud;
}

+ (void)showSuccess:(NSString *)success
{
    [self showSuccess:success toView:nil];
}

+ (void)showError:(NSString *)error
{
    [self showError:error toView:nil];
}

+ (MBProgressHUD *)showMessage:(NSString *)message
{
    return [self showMessage:message toView:nil];
}

+ (void)hideHUDForView:(UIView *)view
{
    if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
    [self hideHUDForView:view animated:YES];
}

+ (void)hideHUD
{
    [self hideHUDForView:nil];
}
@end
